package com.allwees.bs.module.pms.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.allwees.bs.core.model.product.entity.ProductModel;
import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.module.core.wf.WorkflowAware;
import com.allwees.bs.module.pms.wf.ERetailerProductState;
import com.allwees.bs.module.pms.wf.ESupplierProductState;
import com.allwees.core.common.constant.EStatus;
import com.allwees.core.wf.FsmState;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;

/**
 * 商品
 *
 * @author yangylsky
 * @since 2019-08-21 09:26
 */
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@NoArgsConstructor
@TableName("prod_product")
public class Product extends ProductModel implements WorkflowAware {

    private static final long serialVersionUID = -4691643771589044810L;

    /**
     * 编号修改次数
     */
    @Column(columnDefinition = "INT1 UNSIGNED")
    protected Integer noUpdatedCount;

    @JsonIgnore
    private String wfName;

    @JsonIgnore
    @Column(length = 48)
    private String wfUuid;

    @JsonIgnore
    @Column(length = 48)
    private String wfState;

    /**
     * 平台干预因素值，参与score计算
     */
    private Float platformIntervene;

    /**
     * 禁止加价
     */
    private Integer forbiddenAdjustPrice;

    @Override
    @JsonIgnore
    public String getBsKey() {
        return uuid;
    }

    @SuppressWarnings("rawtypes")
    public EBranchType getMerchantWfType() {
        if (StringUtils.isBlank(getWfName())) {
            return null;
        }
        Class<FsmState> wfClass = getWfClass();
        if (ESupplierProductState.class.equals(wfClass)) {
            return EBranchType.SUPPLIER;
        } else if (ERetailerProductState.class.equals(wfClass)) {
            return EBranchType.RETAILER;
        }
        throw new IllegalArgumentException("invalid wfClass:" + wfClass);
    }


    @Override
    public Boolean canEdit() {
        return EStatus.CREATED.equals(getEStatus()) || EStatus.PUTOFF_ONESELF.equals(getEStatus()) || EStatus.AUDIT_REJECTED.equals(getEStatus());
    }

    @Override
    public Boolean isEditing(){
        return EStatus.PUTOFF_EDITING.equals(getEStatus());
    }

    /**
     * 是否可以提审
     *
     * @return
     */
    public boolean canApply() {
        return EStatus.CREATED.equals(getEStatus()) || EStatus.PUTOFF_EDITING.equals(getEStatus()) || EStatus.AUDIT_REJECTED.equals(getEStatus());
    }
}
